package com.yyfax.ph.common.imageselector.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.bumptech.glide.Glide;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.ph.R;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.base.BaseActivity;
import com.yyfax.ph.common.imageselector.adapter.PhotoViewAdapter;
import com.yyfax.ph.common.imageselector.view.PhotoViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * 预览activity
 */
public class PhotoViewActivity extends BaseActivity {
	private static final String TAG = PhotoViewActivity.class.getSimpleName();

	public static final int REQUEST_CODE = 101;

	public static final String EXTRA_PIC_URLS = "mPicUrls";
	public static final String EXTRA_DEFAULT_INDEX = "mInitIndex";
	public static final String EXTRA_VIEW_MODEL = "view_model";

	public static final String RESULT_EXTRA_PHOTO_PATH = "photo_path_key";

	@BindView(R.id.view_pager)
	PhotoViewPager mViewPager;
	@BindView(R.id.tv_title)
	TextView mTitleTv;
	@BindView(R.id.image_right_img_btn)
	ImageButton mRightBtn;
	@BindView(R.id.count_tv)
	TextView mCountTv;
	@BindView(R.id.finish_tv)
	TextView mDoneTv;
	@BindView(R.id.image_btn_back)
	ImageButton imageBtnBack;
	@BindView(R.id.footer)
	RelativeLayout mFooter;

	/**
	 * 存储外面传过来的数据源
	 */
	private List<String> mPicUrls;

	/**
	 * 初始化时显示的图片的下标
	 */
	private int mInitIndex;

	/**
	 * 默认为预览模式
	 */
	private boolean mViewModel = true;

	private ArrayList<String> mSelectedPaths = new ArrayList<>();

	/**
	 * PhotoViewPager的数据源
	 */
	private List<String> mAdapterData = new ArrayList<>();

	public static void startForResult(Activity aty, ArrayList<String> picUrls) {
		Intent intent = new Intent(aty, PhotoViewActivity.class);
		intent.putStringArrayListExtra(EXTRA_PIC_URLS, picUrls);
		intent.putExtra(EXTRA_DEFAULT_INDEX, 0);
		aty.startActivityForResult(intent, REQUEST_CODE);
	}

	/**
	 * @param aty       the source activity
	 * @param picUrls   picture url list
	 * @param position  if -1, then not put to intent bundle
	 * @param viewModel default value is true
	 */
	public static void start(Activity aty, ArrayList<String> picUrls, int position, boolean viewModel) {
		Intent intent = new Intent(aty, PhotoViewActivity.class);
		if (position != -1)
			intent.putExtra(EXTRA_DEFAULT_INDEX, position);
		intent.putStringArrayListExtra(EXTRA_PIC_URLS, picUrls);
		intent.putExtra(EXTRA_VIEW_MODEL, viewModel);//false);
		aty.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {

	}

	@Override
	public int getTopBarBackgroundColor() {
		return YJPH.getInstance().color(R.color.color_3493e8);
	}

	@Override
	public void pretreatmentAfterSetBaseView() {
		if (null != getIntent()) {
			Intent intent = getIntent();
			if (intent.hasExtra(EXTRA_PIC_URLS)) {
				mPicUrls = intent.getStringArrayListExtra(EXTRA_PIC_URLS);
			}
			mInitIndex = intent.getIntExtra(EXTRA_DEFAULT_INDEX, 0);
			mViewModel = intent.getBooleanExtra(EXTRA_VIEW_MODEL, true);

			if (null != mPicUrls) {
				mAdapterData.clear();
				mSelectedPaths.clear();
				mAdapterData.addAll(mPicUrls);
				mSelectedPaths.addAll(mPicUrls);
			}
		}
	}

	@Override
	public int getContentViewRes() {
		return R.layout.im_activity_photoview_layout;
	}

	/**
	 * 初始化View 供子类实现
	 */
	@Override
	public void initContentView() {
		this.setTopBarVisible(false);

		PhotoViewAdapter photoViewAdapter = new PhotoViewAdapter(PhotoViewActivity.this, mAdapterData);
		mViewPager.setAdapter(photoViewAdapter);
		mViewPager.setCurrentItem(mInitIndex);
		mViewPager.setOffscreenPageLimit(0);
		mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				updateView(position);
				if (!mViewModel) {
					mTitleTv.setText((position + 1) + "/" + mAdapterData.size());
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
		updateView(mViewPager.getCurrentItem());

		if (mViewModel) {
			mFooter.setVisibility(View.VISIBLE);
			mCountTv.setText(String.valueOf(mSelectedPaths.size()));
		} else {
			mFooter.setVisibility(View.GONE);
			mRightBtn.setVisibility(View.GONE);
			mDoneTv.setVisibility(View.GONE);
			mCountTv.setVisibility(View.GONE);
			mTitleTv.setText(mInitIndex + 1 + "/" + mAdapterData.size());
		}
	}

	public void done() {
		Intent data = new Intent();
		data.putStringArrayListExtra(RESULT_EXTRA_PHOTO_PATH, mSelectedPaths);
		setResult(RESULT_OK, data);
		finish();
	}

	private void updateView(int index) {
		String selectedPath = mAdapterData.get(index);
		if (mSelectedPaths.contains(selectedPath)) {
			mRightBtn.setImageResource(R.drawable.ic_radio_selected);
		} else {
			mRightBtn.setImageResource(R.drawable.mis_skin_icon_btn_unselected);
		}
	}

	private void checkCurrentPagePath(int index) {
		String selectedPath = mAdapterData.get(index);
		if (mSelectedPaths.contains(selectedPath)) {
			mSelectedPaths.remove(selectedPath);
			mCountTv.setText(String.valueOf(mSelectedPaths.size()));
			mRightBtn.setImageResource(R.drawable.mis_skin_icon_btn_unselected);
		} else {
			mSelectedPaths.add(selectedPath);
			mCountTv.setText(String.valueOf(mSelectedPaths.size()));
			mRightBtn.setImageResource(R.drawable.ic_radio_selected);
		}
	}

	@OnClick({R.id.image_right_img_btn, R.id.finish_tv, R.id.image_btn_back})
	public void onClick(View view) {
		super.onClick(view);
		switch (view.getId()) {
			case R.id.image_right_img_btn:
				checkCurrentPagePath(mViewPager.getCurrentItem());
				break;
			case R.id.image_btn_back:
			case R.id.finish_tv:
				done();
				break;
		}
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return null;
	}

	@Override
	protected void onStop() {
		super.onStop();
		Glide.get(this).clearMemory();
	}
}
